﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace fqControl
{
    public class tTabContorl:TabControl
    {
        private int iconWidth = 15;
        private int iconHeight = 13;
        private Image icon = null;
       // private Brush color = Brushes.White;  //选项卡的背景色
        public tTabContorl():base()
        {
            this.ItemSize = new Size(50, 28);      //设置选项卡标签的大小,可改变高，不可改变宽

           // this.Appearance = TabAppearance.Buttons;  //选项卡的显示模式

            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            icon = fqControl.Properties.Resources.Close1;
            iconWidth = icon.Width;
            iconHeight = icon.Height;
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            Graphics g = e.Graphics;
            Rectangle r = GetTabRect(e.Index);

            //g.FillRectangle(color, r);  //改变选项卡标签的背景色
            string title = this.TabPages[e.Index].Text;
            g.DrawString(title, this.Font, new SolidBrush(Color.Black), new PointF(r.X + 3, r.Y + 6));//PointF选项卡标题的位置

            r.Offset(r.Width - iconWidth - 3, 2);
            g.DrawImage(icon, new Point(r.X - 2, r.Y + 2));//选项卡上的图标的位置
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            Point p = e.Location;
            Rectangle r = GetTabRect(this.SelectedIndex);
            r.Offset(r.Width - iconWidth - 3, 2);
            r.Width = iconWidth;
            r.Height = iconHeight;

            if (r.Contains(p)) //点击特定区域时才发生
            {
                //关闭选项卡中加载的窗口
                foreach (Form cform in Application.OpenForms)
                {
                    if (cform.Name == this.SelectedTab.Name)
                    {
                        cform.Close();
                        break;
                    }
                }
                this.TabPages.Remove(this.SelectedTab);
            }

        }
    }
}
